import {
	reactive,
	ref,
	toRefs,
	getCurrentInstance,
	watch,
	onMounted
} from "vue"
import {
	onReady,
	onLoad,
	onShow,
	onShareAppMessage
} from '@dcloudio/uni-app'

import store from '@/store/index.js'; //需要引入store

export default function() {
	const {
		proxy
	} = getCurrentInstance()
	const houseType = ref();
	const fenliId = ref();
	const list = ref([])
	const tagActive = reactive({})
	const menuActive = reactive({})
	const searchValue = reactive({})
	const searchInputValue = reactive({})
	watch(
	   searchInputValue,
	  (newValue, oldValue) => {
	    setMenuActive();
	  }
	);
	onMounted(() => {
		// console.log('house-filter-item-onMounted');
		getData();
	})
	const getData = async () => {
		let key = `houseFilter${fenliId.value}${houseType.value}`;
		let data = store.getters.get_houseFilter[key];
		if (!data) {
			await proxy.$u.api.houseFilter({
				fenli_id: fenliId.value,
				house_type: houseType.value,
				// p_id:,
			}).then(res => {
				let {
					code,msg
				} = res;
				if(code == 200){
					data = res.data.data;
					store.commit('set_houseFilterItem',{
						key:key,
						value:data
					})
				}else{
					uni.$u.toast(msg || '错误');
				}
			})
		}
		// console.log(data);
		list.value = data;
		setDefaultValue()
	}
	const setDefaultValue = () =>{
		list.value.forEach(v => {
			if (v.search_type == 4) {
				searchValue[v.filter_name] = ['', ''];
				searchInputValue[v.filter_name] = ['', ''];
			}else if(v.search_type == 2){
				searchValue[v.filter_name] = [];
			}else {
				searchValue[v.filter_name] = '';
			}
		})
	}
	const setValue = (d) => {
		// console.log('setValue');
		for(let filter_name in searchValue){
			let p_item = list.value.find(v=>v.filter_name == filter_name);
			if(d[filter_name]){
				if(p_item.search_type == 4){
					let [min, max] = d[filter_name];
					let item = p_item.children.find(v=>v.min_value == min && v.max_value == max)
					// console.log(item);
					if(item){
						searchValue[filter_name] = d[filter_name];
						searchInputValue[filter_name] = ['', ''];
					}else{
						searchValue[filter_name] = ['', ''];
						searchInputValue[filter_name] = [min, max];
					}
				}else{
					searchValue[filter_name] = d[filter_name];
				}
			}
			if(d[filter_name] === ''){
				if(p_item.search_type == 4){
					searchValue[filter_name] = ['', ''];
					searchInputValue[filter_name] = ['', ''];
				}else{
					searchValue[filter_name] = '';
				}
			}
		}
		setTagActive();
		setMenuActive();
	}

	const tagClick = (item, parentItem) => {
		// "search_type": 1, //搜索类型【1-ID单选（传house_tag_id）2-ID多选（传[house_tag_id,house_tag_id]）3-单个值(传min_value)4-区间值(传
		if (parentItem.search_type == 2) {
			if (!searchValue[parentItem.filter_name]) {
				searchValue[parentItem.filter_name] = []
			}
			if (searchValue[parentItem.filter_name].includes(item.house_tag_id)) {
				searchValue[parentItem.filter_name] = searchValue[parentItem.filter_name].filter(v => v != item.house_tag_id)
			} else {
				searchValue[parentItem.filter_name].push(item.house_tag_id)
			}
		}
		if (parentItem.search_type == 1) {
			if (searchValue[parentItem.filter_name] == item.house_tag_id) {
				searchValue[parentItem.filter_name] = ''
			} else {
				searchValue[parentItem.filter_name] = item.house_tag_id
			}
		}
		if (parentItem.search_type == 3) {
			if (searchValue[parentItem.filter_name] == item.min_value) {
				searchValue[parentItem.filter_name] = ''
			} else {
				searchValue[parentItem.filter_name] = item.min_value
			}
		}
		if (parentItem.search_type == 4) {
			if (searchValue[parentItem.filter_name] && searchValue[parentItem.filter_name][0] == item.min_value && searchValue[parentItem.filter_name][1] == item.max_value) {
				searchValue[parentItem.filter_name] = ['', '']
			} else {
				searchValue[parentItem.filter_name] = [item.min_value, item.max_value]
			}
		}
		setTagActive()
		setMenuActive();
	}
	const setTagActive = () => {
		list.value.forEach(item => {
			tagActive[item.house_tag_id] = {};
			let filter_name = item.filter_name;
			item.children.forEach(v => {
				let {
					house_tag_id,
					min_value,
					max_value
				} = v;
				try {
					if (item.search_type == 2) {
						tagActive[item.house_tag_id][house_tag_id] = searchValue[filter_name]
							.includes(house_tag_id)
					}
					if (item.search_type == 1) {
						tagActive[item.house_tag_id][house_tag_id] = searchValue[filter_name] == house_tag_id
					}
					if (item.search_type == 3) {
						tagActive[item.house_tag_id][house_tag_id] = searchValue[filter_name] == min_value
					}
					if (item.search_type == 4) {
						tagActive[item.house_tag_id][house_tag_id] = searchValue[filter_name][0] == min_value && searchValue[filter_name][1] == max_value
					}
				} catch (e) {
					tagActive[item.house_tag_id][house_tag_id] = false
				}
			})
		})
	}
	const setMenuActive = ()=>{
		list.value.forEach(item => {
			let flag = false
			let filter_name = item.filter_name;
			if(searchValue[filter_name]){
				let value = searchValue[filter_name];
				if(Array.isArray(value)){
					let _arr = value.filter(v=>!!v);
					if(_arr.length!=0){
						flag = true;
					}
				}else{
					flag = true;
				}
			}
			if(searchInputValue[filter_name] && (searchInputValue[filter_name][0] || searchInputValue[filter_name][1])){
				flag = true;
			}
			menuActive[item.house_tag_id] = flag;
		})
	}
	const setReset = () => {
		setDefaultValue();
		setTagActive();
		setMenuActive();
		let _params = JSON.parse(JSON.stringify(searchValue));
		return _params
	}
	const setSubmit = () => {
		let _params = JSON.parse(JSON.stringify(searchValue));
		for (let filter_name in searchInputValue) {
			let [min, max] = searchInputValue[filter_name];
			if (min || max) {
				_params[filter_name] = [min, max]
			}
		}
		return _params
	}
	return {
		houseType,
		fenliId,
		list,
		tagActive,
		menuActive,
		searchValue,
		searchInputValue,
		setReset,
		setSubmit,
		tagClick,
		setTagActive,
		setValue
	}
}